package com.cskaoyan.controller;


import com.cskaoyan.bean.category.Category;
import com.cskaoyan.bean.category.bo.CategoryCreateBoL2;
import com.cskaoyan.bean.category.bo.CategoryDeleteBoL1;
import com.cskaoyan.bean.category.vo.CategoryDataVo;
import com.cskaoyan.bean.category.vo.L1Vo;
import com.cskaoyan.bean.BaseRespVo;
import com.cskaoyan.service.category.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 创建日期: 2022/05/05 17:09
 *
 * @author JinLu Zhou
 */
@RestController
@RequestMapping("admin/category")
public class CategoryController {

    @Autowired
    CategoryService categoryService;

    @GetMapping("list")
    public BaseRespVo list() {
        return categoryService.selectAllCategory();
    }


    @RequestMapping("l1")
    public BaseRespVo<CategoryDataVo<L1Vo>> l1() {
        BaseRespVo<CategoryDataVo<L1Vo>> categoryDataVoBaseRespVo = new BaseRespVo<>();

        CategoryDataVo<L1Vo> categoryDataVo = new CategoryDataVo<>();
        categoryDataVo.setPage(1);
        categoryDataVo.setLimit(10);
        categoryDataVo.setPagers(10);
        List<L1Vo> list = categoryService.selectAllCategoryIdAndName();
        categoryDataVo.setList(list);
        Integer total = categoryService.selectAllCategoryNumber();
        categoryDataVo.setTotal(total);

        categoryDataVoBaseRespVo.setData(categoryDataVo);
        categoryDataVoBaseRespVo.setErrno(0);
        categoryDataVoBaseRespVo.setErrmsg("成功");

        return categoryDataVoBaseRespVo;
    }

    @RequestMapping("create")
    public BaseRespVo<Category> create(@RequestBody CategoryCreateBoL2 categoryCreateBo) {
        Category category = categoryService.createCategory(categoryCreateBo);
        BaseRespVo<Category> categoryBaseRespVo = new BaseRespVo<>();
        categoryBaseRespVo.setData(category);
        categoryBaseRespVo.setErrmsg("成功");
        categoryBaseRespVo.setErrno(0);

        return categoryBaseRespVo;


    }

    @RequestMapping("update")
    public BaseRespVo update(@RequestBody Category category) {
        return categoryService.update(category);
    }

    @RequestMapping("delete")
    public BaseRespVo delete(@RequestBody CategoryDeleteBoL1 categoryDeleteBoL1) {
        return categoryService.delete(categoryDeleteBoL1);
    }

}
